package com.zsg.androidsipphone.ui.fragment;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.textfield.TextInputEditText;
import com.zsg.androidsipphone.R;
import com.zsg.androidsipphone.adapter.MyRecyclerAdapter;
import com.zsg.androidsipphone.bean.Contact;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author zsg
 */
public class CenterFragment extends Fragment {

    private MyRecyclerAdapter adapter;
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_center, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        RecyclerView recyclerView = requireView().findViewById(R.id.recycler_account_box);
        FloatingActionButton actionButton = requireView().findViewById(R.id.fab_add_contact_person);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        adapter = new MyRecyclerAdapter();
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);
        getLocalContact();
        actionButton.setOnClickListener((view1 ->showAddContactDialog()));
    }


    /**
     * 读取本地联系人信息
     */
    private void getLocalContact() {
        SharedPreferences preferences = requireContext().getSharedPreferences("myContact",Context.MODE_PRIVATE);
        Map<String,?> map = preferences.getAll();
        List<Contact> contacts = new ArrayList<>();
        for (Map.Entry<String, ?> enter: map.entrySet()){
            Contact contact = new Contact(enter.getKey(),preferences.getString(enter.getKey(),""));
            contacts.add(contact);
        }
        //提交联系人数据
        adapter.setAdapterData(contacts);
    }

    /**
     * 弹出添加联系人弹窗
     */
    private void showAddContactDialog(){
        View view = View.inflate(requireContext(),R.layout.dialog_input,null);
        TextInputEditText input1 = view.findViewById(R.id.input_text1);
        TextInputEditText input2 = view.findViewById(R.id.input_text2);
        new AlertDialog.Builder(requireContext())
                .setTitle("联系人信息")
                .setView(view)
                .setPositiveButton("添加", (dialogInterface, i) -> {
                    String text1 = Objects.requireNonNull(input1.getText()).toString();
                    String text2 = Objects.requireNonNull(input2.getText()).toString();
                    if (!text1.isEmpty() && !text2.isEmpty()){
                        //保存联系人信息
                        requireContext().getSharedPreferences("myContact", Context.MODE_PRIVATE)
                                .edit()
                                .putString(text1,text2)
                                .apply();
                        //更新列表
                        List<Contact> contacts = new ArrayList<>();
                        Contact contact = new Contact(text1,text2);
                        contacts.add(contact);
                        adapter.addAdapterData(contacts);
                    }else {
                        Toast.makeText(requireContext(),"请完善联系人信息！",Toast.LENGTH_SHORT).show();
                    }

                })
                .setNegativeButton("取消",null)
                .show();
    }



}